package cn.smile.web;

import cn.smile.Application;
import cn.smile.mvc.SpringMvcConfig;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FrameworkServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 毛苏宇
 * User: me
 * Date: 2021/4/22
 * Time: 12:59
 * Description:
 */
public class WebXmlConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    /**
     * 加载spring配置类
     *
     * @return
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        //spring基础配置类
        return new Class[]{Application.class};
    }

    /**
     * 加载SpringMvc配置
     *
     * @return
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    /**
     * 配置DispatcherServlet前端控制器拦截的URL
     *
     * @return
     */
    @Override
    protected String[] getServletMappings() {
        //设置"/"为匹配所有请求
        return new String[]{"/"};
    }

    @Override
    protected FrameworkServlet createDispatcherServlet(WebApplicationContext servletAppContext) {
        DispatcherServlet dispatcherServlet=new DispatcherServlet();
        //设置应用程序上下文
        dispatcherServlet.setApplicationContext(servletAppContext);
        //自定义异常处理，默认为false
        dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
        return dispatcherServlet;
    }

}
